package top.api.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.convert.PeriodUnit;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import top.api.common.ResponseResult;
import top.api.domain.dto.AddMenuDto;
import top.api.domain.dto.UpdateMenuDto;
import top.api.service.MenuService;

@RestController
@RequestMapping("system/menu")
@Api(tags = "菜单管理")
public class MenuController {
    @Autowired
    private MenuService menuService;

    @GetMapping("/list")
    @ApiOperation(value = "菜单查询")
    @PreAuthorize("@ps.hasPermission('system:menu:query')") // 菜单查询 权限校验
    public ResponseResult list(String menuName,String status){
        return menuService.getMenuListByMenuNameAndStatus(menuName,status);
    }

    @PostMapping
    @ApiOperation(value = "新增菜单")
    @PreAuthorize("@ps.hasPermission('system:menu:add')") // 菜单新增 权限校验
    public ResponseResult add(@RequestBody AddMenuDto addMenuDto){
        return menuService.addMenu(addMenuDto);
    }

    @GetMapping("/{id}")
    @ApiOperation(value = "查询菜单详情")
    @PreAuthorize("@ps.hasPermission('system:menu:query')") // 菜单查询 权限校验
    public ResponseResult getMenuInfo(@PathVariable Long id){
        return menuService.getMenuInfo(id);
    }

    @PutMapping
    @ApiOperation(value = "修改菜单")
    @PreAuthorize("@ps.hasPermission('system:menu:edit')") // 修改菜单 权限校验
    public ResponseResult updateMenu(@RequestBody UpdateMenuDto updateMenuDto){
        return menuService.updateMenu(updateMenuDto);
    }

    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除菜单")
    @PreAuthorize("@ps.hasPermission('system:menu:remove')") // 删除菜单 权限校验
    public ResponseResult deleteMenu(@PathVariable Long id){
        return menuService.deleteMenu(id);
    }

    /**
     * 获取菜单树接口
     * @return
     */
    @GetMapping("/treeselect")
    @ApiOperation(value = "获取菜单树接口")
    @PreAuthorize("@ps.hasPermission('system:menu:query')") // 菜单查询 权限校验
    public ResponseResult treeSelect(){
        return menuService.treeSelect();
    }

    @GetMapping("/roleMenuTreeselect/{id}")
    @ApiOperation(value = "加载对应角色菜单列表树接口")
    @PreAuthorize("@ps.hasPermission('system:menu:query')") // 菜单查询 权限校验
    public ResponseResult roleMenuTreeselect(@PathVariable Long id){
        return menuService.roleMenuTreeselect(id);
    }

}
